package com.aiju.cash.ui.pay;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.PayApi;
import com.aiju.cash.ui.pay.entity.Pay;
import com.aiju.cash.ui.pay.util.AdditionMoney;
import com.aiju.utils.L;
import com.aiju.utils.PriceUtil;
import com.aiju.utils.T;
import com.aiju.widgit.dialog.SimpleDialog;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2016-12-06 09:49
 * @version: V1.0
 * @Description: 现金收款
 */
public class PayMoneyActivity extends BaseTitleActivity implements View.OnClickListener {

    private AdditionMoney additionMoney;

    private Pay pay;

    private TextView moneyTextView, changeTextView, receivedTextView;

    private double change;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_money);
        setCenterTitle("现金收款");
        setRightTitle("取消收款");

        findViewById(R.id.tv_pay_1).setOnClickListener(this);
        findViewById(R.id.tv_pay_2).setOnClickListener(this);
        findViewById(R.id.tv_pay_3).setOnClickListener(this);
        findViewById(R.id.tv_pay_4).setOnClickListener(this);
        findViewById(R.id.tv_pay_5).setOnClickListener(this);
        findViewById(R.id.tv_pay_6).setOnClickListener(this);
        findViewById(R.id.tv_pay_7).setOnClickListener(this);
        findViewById(R.id.tv_pay_8).setOnClickListener(this);
        findViewById(R.id.tv_pay_9).setOnClickListener(this);
        findViewById(R.id.tv_pay_0).setOnClickListener(this);
        findViewById(R.id.tv_pay_point).setOnClickListener(this);
        findViewById(R.id.tv_pay_c).setOnClickListener(this);
        findViewById(R.id.iv_pay_back).setOnClickListener(this);
        findViewById(R.id.iv_pay_add).setOnClickListener(this);
        findViewById(R.id.tv_pay_pay).setOnClickListener(this);

        additionMoney = new AdditionMoney();
        additionMoney.setMoneyChange(new AdditionMoney.OnMoneyChange() {
            @Override
            public void onMoneyChange(String favorable, String printAllMoney, String totalMoney, String receivableMoney) {
                receivedTextView.setText(totalMoney);
                change = Double.valueOf(totalMoney) - Double.valueOf(pay.getPrice());
                changeTextView.setText(PriceUtil.formatDouble(change));
                if (change < 0) {
                    changeTextView.setTextColor(Color.parseColor("#ec4545"));
                } else {
                    changeTextView.setTextColor(Color.parseColor("#1e82d2"));
                }
                pay.setActualPrice(totalMoney);
                pay.setChangePrice(PriceUtil.formatDouble(change));
            }
        });

        pay = (Pay) getIntent().getExtras().getSerializable("pay");
        moneyTextView = (TextView) findViewById(R.id.tv_money_money);
        changeTextView = (TextView) findViewById(R.id.tv_money_change);
        receivedTextView = (TextView) findViewById(R.id.tv_money_received);
        moneyTextView.setText(pay.getPrice());
        receivedTextView.setText(pay.getPrice());
        changeTextView.setText("0.00");
        ((TextView) findViewById(R.id.tv_pay_pay)).setText(getString(R.string.money));
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SimpleDialog(PayMoneyActivity.this)
                        .setTitileText("是否取消收款")
                        .setLeftText("否")
                        .setRightText("是")
                        .setOnSimpleClick(new SimpleDialog.OnSimpleClick() {
                            @Override
                            public void onCancel() {
                            }

                            @Override
                            public void onSubmit() {
                                finish();
                            }
                        }).setLayoutParams().show();

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_pay_0:
                additionMoney.onKeyDown(0);
                break;
            case R.id.tv_pay_1:
                additionMoney.onKeyDown(1);
                break;
            case R.id.tv_pay_2:
                additionMoney.onKeyDown(2);
                break;
            case R.id.tv_pay_3:
                additionMoney.onKeyDown(3);
                break;
            case R.id.tv_pay_4:
                additionMoney.onKeyDown(4);
                break;
            case R.id.tv_pay_5:
                additionMoney.onKeyDown(5);
                break;
            case R.id.tv_pay_6:
                additionMoney.onKeyDown(6);
                break;
            case R.id.tv_pay_7:
                additionMoney.onKeyDown(7);
                break;
            case R.id.tv_pay_8:
                additionMoney.onKeyDown(8);
                break;
            case R.id.tv_pay_9:
                additionMoney.onKeyDown(9);
                break;
            case R.id.tv_pay_point:
                additionMoney.onKeyDown(AdditionMoney.POINT);
                break;
            case R.id.tv_pay_c:
                additionMoney.onKeyDown(AdditionMoney.CLEAR);
                break;
            case R.id.tv_pay_pay:
                pay(v);
                break;
            case R.id.iv_pay_back:
                additionMoney.onKeyDown(AdditionMoney.BACK);
                break;
            case R.id.iv_pay_add:
                additionMoney.onKeyDown(AdditionMoney.ADD);
                break;
        }
    }

    private void pay(View v) {
        if (change < 0 && !pay.getPrice().equals("0.00")) {
            T.ToastShort("付款失败，请顾客付款" + Math.abs(change) + "元");
            return;
        }
        mDialogHelper.showLoadingProgressDialog();
        new PayApi().pay(pay.getStoreId(), pay.getSubject(), pay.getPrice(),
                pay.getSumPrice(), mUser.getOperatorId(), mUser.getUserName(), pay.getPrivType(), pay.getPrivPrice(),
                pay.getShouldPrice(), pay.getActualPrice(), pay.getChangePrice(),
                pay.getRoundPrice(), pay.getMemberPhone(), pay.getDetail(), new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        T.ToastNetError();
                        mDialogHelper.dismissProgressDialog();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        mDialogHelper.dismissProgressDialog();
                        DataJson json = new DataJson(response);
                        if (json.isSuccess()) {
                            L.d("Lifecycle", "PayMoneyActivity");
                            pay.setChangePrice(PriceUtil.formatDouble(change));
                            Intent intent = new Intent(PayMoneyActivity.this, PaySuccessActivity.class);
                            intent.putExtra("serialNum", json.getData().toString());
                            intent.putExtra("money", pay.getPrice());
                            startActivity(intent);
                        } else {
                            T.ToastShort(json.getMsg());
                        }
                    }
                });
    }
}